/**
AirwayPathfinder for X-Plane Flight Simulator. Calculates the shortest path between two NavAids. 
Copyright (C) 2011  Nils Buletta

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 **/

package de.buletta.airwaypathfinder;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Point2D;

import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.Waypoint;
import org.jdesktop.swingx.mapviewer.WaypointPainter;

/**
 * Paints the Waypoints in the calculated path and the connections between them.
 * 
 * @author Nils Buletta
 * @version 0.1
 */
public class PathWaypointPainter<T extends JXMapViewer> extends
		WaypointPainter<T> {
	public PathWaypointPainter() {
		super();
	}

	@Override
	protected void doPaint(Graphics2D g, T map, int w, int h) {
		super.doPaint(g, map, w, h);

		g = (Graphics2D) g.create();

		// convert from viewport to world bitmap
		Rectangle rec = map.getViewportBounds();
		g.translate(-rec.x, -rec.y);
		g.setColor(Color.RED);

		for (Waypoint wp : getWaypoints()) {
			EarthNav earthNav = (EarthNav) wp;
			if (earthNav.getPredecessor() != null) {
				Point2D currentPoint = map.getTileFactory().geoToPixel(
						earthNav.getPosition(), map.getZoom());
				Point2D predecessorPoint = map.getTileFactory().geoToPixel(
						earthNav.getPredecessor().getPosition(), map.getZoom());

				g.drawLine((int) currentPoint.getX(),
						(int) currentPoint.getY(),
						(int) predecessorPoint.getX(),
						(int) predecessorPoint.getY());
			}

		}
		g.dispose();
	}

}
